package ca.etsmtl.log730.projet2.connectors.msg.document;

import java.util.UUID;

import ca.etsmtl.log730.projet2.connectors.clientserver.RequestManager;
import ca.etsmtl.log730.projet2.connectors.clientserver.nodes.DocumentMap;
import ca.etsmtl.log730.projet2.connectors.clientserver.nodes.DocumentMap.DocumentListener;
import ca.etsmtl.log730.projet2.connectors.msg.Request;
import ca.etsmtl.log730.projet2.connectors.msg.RequestReply;
import ca.etsmtl.log730.projet2.users.User;

public class AccessCMD extends Request {

	@Override
	public void apply(DocumentMap dm) {
		super.apply(dm);
		if(action == Action.REMOVE) {
			if(mode == Mode.EDIT) {
				dm.getEditorQueue(docId).remove(user);
				
//				dm.get(docId).getAccess().getEditors().remove(user);
			} else if(mode == Mode.VIEW) {
				dm.getViewers(docId).remove(user);
//				dm.get(docId).getAccess().getViewers().remove(user);
			}
		} else if(action == Action.ADD) {
			if(mode == Mode.EDIT) {
				dm.getEditorQueue(docId).add(user);
//				dm.get(docId).getAccess().getEditors().add(user);
			} else if(mode == Mode.VIEW) {
				dm.getViewers(docId).add(user);
//				dm.get(docId).getAccess().getViewers().add(user);
			}
		}
		for(DocumentListener dl: dm.getDocumentListeners()) {
			dl.updated(getDocId(), null);
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 6766833442797571724L;
	private UUID docId;
	private User user;
	private Action action;
	private Mode mode;
	public AccessCMD(UUID ui, User u, Action a, Mode m) {
		setDocId(ui);
		setUser(u);
		setAction(a);
		setMode(m);
	}
	public Mode getMode() {
		return mode;
	}

	public void setMode(Mode mode) {
		this.mode = mode;
	}
	public Action getAction() {
		return action;
	}
	public void setAction(Action action) {
		this.action = action;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UUID getDocId() {
		return docId;
	}
	public void setDocId(UUID docId) {
		this.docId = docId;
	}
	public static enum Action {
		REMOVE, ADD;
	}
	
	public static enum Mode {
		VIEW, EDIT;
	}

	@Override
	public RequestReply createRequestReply(RequestManager rm) {
		return new RequestReply.Accepted(this);
	}

}
